package com.ap.core.jbehave.submitrtp;

import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ap.core.cxf.jaxb.common.DeliveryType;
import com.ap.core.cxf.jaxb.managertp.SubmitRTP1Request;
import com.ap.core.cxf.jaxb.managertp.SubmitRTP1Response;
import com.ap.core.cxf.jaxb.managertp.SubmitRTPMessageType;
import com.ap.core.cxf.jaxb.rtp.TransactionCheckOutType;
import com.ap.core.jbehave.BaseTransitionRTPSteps;
import com.ap.core.jbehave.StoryContext;
import com.ap.core.util.JaxbUtil;
import com.ap.core.util.Nullable;
import com.ap.core.util.TypedKey;
import com.core.ap.wsdl.managertpservice_1.ManageRTP1PortType;
import com.core.ap.wsdl.managertpservice_1.SubmitRTP1Fault;

/**
 * This class contains the steps used by the test harness JBehave instances.
 */
public class SubmitRTPBRNDeliverySteps extends BaseTransitionRTPSteps {

	private static final TypedKey<Nullable<SubmitRTP1Response>> SubmitRTP1Response = new TypedKey<>(
			"ResponseRTPType");

	private static final Logger LOG = LoggerFactory
			.getLogger(SubmitRTPSteps.class);

	private SubmitRTP1Request submitRTPRequest;

	public SubmitRTPBRNDeliverySteps(StoryContext storyContext) {
		super(storyContext);
	}

	@Given("a Consumer chooses to make payment using AP service through e-com site to purchase a product with delivery type as 'Delivery to Address' or 'Collect at Store'")
	public void givenSUBMITRTPFORBRNSubmitted() {
		LOG.info("Given a Consumer chooses to make payment using AP service through e-com site to purchase a product with delivery type as 'Delivery to Address' or 'Collect at Store'");
	}

	@When("Distributor submits RTP to AP Server")
	public void whenAPCoreReceviedSUBMITRTPFORBRN() throws SubmitRTP1Fault {

		LOG.info("When Distributor submits RTP to AP Server");
		final ManageRTP1PortType singnedPort = factory
				.getBean(ManageRTP1PortType.class);
		submitRTPRequest = createSubmitRTPRequest(SubmitRTPMessageType.SUBMITRTPFORBRN);
		submitRTPRequest.getRTPSummary().setTxCheckout(
				TransactionCheckOutType.NRMLCHK);
		submitRTPRequest.getRTPSenderInfo().getDistributorInfo()
				.setDistributorId("000001");
		submitRTPRequest.getRTPSenderInfo().getDetailMerchantInfo()
				.getMerchantInfo().setMerchantId("000001");
		submitRTPRequest.getDelivery().setDeliveryType(DeliveryType.COLLST);
		JaxbUtil.newInstance().javaToXML(SubmitRTP1Request.class,
				submitRTPRequest);
		SubmitRTP1Response responseRTPType = singnedPort
				.submitRTP1(submitRTPRequest);

		JaxbUtil.newInstance().javaToXML(SubmitRTP1Response.class,
				responseRTPType);
		storyContext
				.put(SubmitRTP1Response, Nullable.nullable(responseRTPType));
	}

	@Then("AP server should send Response to RTP to the Distributor which would invoke the mobile app on consumer's device")
	public void thenDeliveryAddressValidated() {
		LOG.info("AP server should send Response to RTP to the Distributor which would invoke the mobile app on consumer's device");

		Nullable<SubmitRTP1Response> responseRTPType = storyContext
				.get(SubmitRTP1Response);

		doSubmitRTPResponseAssertions(responseRTPType, submitRTPRequest);		
	}
}
